package com.wn.class30.service.impl;

import com.wn.class30.entity.*;
import com.wn.class30.mapper.MyorderDao;
import com.wn.class30.mapper.UserDao;
import com.wn.class30.service.OrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description: OrderServiceImpl 方法是
 * @author: jzy
 * @date: 2020/4/2 4:34 PM
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private MyorderDao myorderDao;
    @Resource
    private UserDao userDao;
    @Override
    public List<Myorder> selectOrders(Map<String,String> map) throws Exception {
        List<Myorder> myorders = myorderDao.selectByExample(getOrderExample(map));
        return myorders;
    }

    @Override
    public Long getOrderCount(Map<String,String> map) throws Exception {
        return myorderDao.countByExample(getOrderExample(map));
    }

    @Override
    public List<Myorder> queryOrderByUserId(Integer userid) throws Exception {
        MyorderExample myorderExample = new MyorderExample();
        MyorderExample.Criteria criteria = myorderExample.createCriteria();
        criteria.andUserIdEqualTo(userid);
        List<Myorder> myorders = myorderDao.queryMyorderByExample(myorderExample);
        return myorders;
    }

    @Override
    public void deleteOrderByOid(Integer oid) throws Exception {
        myorderDao.deleteByPrimaryKey(oid);
    }

    @Override
    public Myorder selectMyOrderById(Integer myOrderId) throws Exception {
        return myorderDao.selectByPrimaryKey(myOrderId);
    }

    @Override
    public void updateMyOrderById(Myorder myorder) throws Exception {
        myorderDao.updateByPrimaryKey(myorder);
    }

    @Override
    public Myorder queryOrderByOrderId(Integer orderid) throws Exception {
        Myorder myorder = myorderDao.selectByPrimaryKey(orderid);
        return myorder;
    }

    @Override
    public List<Myorder> selectMyOrderByAllOrderId(Integer allOrderId) throws Exception {
        MyorderExample myorderExample = new MyorderExample();
        myorderExample.createCriteria().andAllorderIdEqualTo(allOrderId);
        return myorderDao.selectByExample(myorderExample);
    }

    public MyorderExample getOrderExample(Map<String,String> map)throws Exception{
        Long page = Long.parseLong(map.get("page"));
        Integer limit = Integer.parseInt(map.get("limit"));
        String myorderNumber = map.get("myorderNumber");
        String username = map.get("username");
        String orderStatus =map.get("orderStatus");
        String businessId = map.get("businessId");
        String myorderId = map.get("myorderId");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = map.get("startDate");
        String endDate = map.get("endDate");
        MyorderExample example = new MyorderExample();
        example.setLimit(limit);
        example.setOffset(page);
        MyorderExample.Criteria criteria = example.createCriteria();
        if(!"".equals(businessId) && businessId != null){
            criteria.andBussinessIdEqualTo(Integer.parseInt(businessId));
        }
        if(!"".equals(myorderId) && myorderId != null){
            criteria.andMyorderIdEqualTo(Integer.parseInt(myorderId));
        }
        if(!"".equals(myorderNumber) && myorderNumber != null){
            criteria.andMyorderNumberLike("%"+myorderNumber+"%");
        }
        if(!"".equals(username) && username != null){
            UserExample userExample = new UserExample();
            UserExample.Criteria userExampleCriteria = userExample.createCriteria();
            userExampleCriteria.andUsernameLike("%"+username+"%");
            List<User> users = userDao.selectByExample(userExample);
            if(users.size() > 0){
                for (User user : users) {
                    criteria.andUserIdEqualTo(user.getUserId());
                }
            }else{
                criteria.andUserIdEqualTo(-1);
            }
        }

        if(!"".equals(orderStatus) && orderStatus != null && Integer.parseInt(orderStatus) != -1){
            criteria.andMyorderStatusEqualTo(Integer.parseInt(orderStatus));
        }

        if(!"".equals(startDate) && startDate != null){
            criteria.andMyorderTimeGreaterThanOrEqualTo(simpleDateFormat.parse(startDate));
        }

        if(!"".equals(endDate) && endDate != null){
            Date date = simpleDateFormat.parse(endDate);
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(Calendar.DAY_OF_MONTH, 1);
            Date time = c.getTime();
            String format = simpleDateFormat.format(time);
            criteria.andMyorderTimeLessThanOrEqualTo(simpleDateFormat.parse(format));
        }
        example.setOrderByClause("`myorder_time` DESC");
        return example;
    }
}